using Nop.Core.Plugins;
using Nop.Plugin.ActiveForever.ActiveChat.Data;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Web.Framework.Menu;
using Nop.Web.Framework.Web;
using System.Collections.Generic;
using System.Web.Routing;
using Telerik.Web.Mvc.UI;

namespace Nop.Plugin.ActiveForever.ActiveChat
{
	public class ActiveChatPlugin : BasePlugin, IWidgetPlugin, IAdminMenuPlugin
	{
		private readonly ISettingService _settingService;
		private readonly ActiveChatObjectContext _activeChatObjectContext;
        public ActiveChatPlugin(ISettingService settingService, ActiveChatObjectContext activeChatObjectContext)
		{
			_settingService = settingService;
			_activeChatObjectContext = activeChatObjectContext;
		}

		public virtual bool Authenticate()
		{
			return true;
		}

	    public SiteMapNode BuildMenuItem()
	    {
            SiteMapNode rootNnode = new SiteMapNode { Visible = true, Title = "ActiveChat" };

            SiteMapNode operatorNode = new SiteMapNode { Visible = true, Title = "Operator", Url = "/activechat" };
            rootNnode.ChildNodes.Add(operatorNode);

            SiteMapNode settingsNode = new SiteMapNode { Visible = true, Title = "Settings", Url = "/Admin/Widget/ConfigureWidget?systemName=ActiveForever.ActiveChat" };
            rootNnode.ChildNodes.Add(settingsNode);

            SiteMapNode helpNode = new SiteMapNode { Visible = true, Title = "Help", Url = "/activechathelp" };
            rootNnode.ChildNodes.Add(helpNode);

            return rootNnode;
	    }

		public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
		{
			actionName = "Configure";
			controllerName = "ActiveChat";
			RouteValueDictionary routeValueDictionaries = new RouteValueDictionary
			{
				{ "Namespaces", "Nop.Plugin.ActiveForever.ActiveChat.Controllers" },
				{ "area", null }
			};
			routeValues = routeValueDictionaries;
		}

		public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
		{
			actionName = "PublicInfo";
			controllerName = "ActiveChat";
			RouteValueDictionary routeValueDictionaries = new RouteValueDictionary
			{
				{ "Namespaces", "Nop.Plugin.ActiveForever.ActiveChat.Controllers" },
				{ "area", null },
				{ "widgetZone", widgetZone }
			};
			routeValues = routeValueDictionaries;
		}

		public IList<string> GetWidgetZones()
		{
			return new List<string>
			{
				"body_end_html_tag_before"
			};
		}

		public override void Install()
		{
			ActiveChatSettings activeChatSetting = new ActiveChatSettings
			{
				OperatorWelcomeMessage = "Hello! How can I help you today?",
				ClientWelcomeMessageOnline = "<strong>Have a question? We've got answers!</strong><br />Complete the information below then click 'Start Chat' to speak with a live representative...",
				ClientWelcomeMessageOffline = "<strong>Have a question? We've got answers!</strong><br />No operators are available to take your chat at this time. Please feel free to leave a message below...",
				ChatWindowWidth = 800,
				ChatWindowHeight = 600,
				OfflineEmailAccountId = 1,
				ChargeId = string.Empty,
				PostChatEmail = "",
				PostChatEmailSubject = "",
				EnablePostChatEmail = false,
				StaticFileName = string.Empty,
				WidgetImage = string.Empty
			};
			_settingService.SaveSetting(activeChatSetting);
			_activeChatObjectContext.Install();
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.TitleOnline", "Live Chat - Online");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.TitleOffline", "Live Chat - Offline");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldName", "Your name");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldEmail", "E-mail");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldMessage", "Message");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.LeaveAMessage", "Leave A Message");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.Close", "Close");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.OperatorTyping", "Operator is typing...");
			this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ActiveChat.Send", "Send");
			base.Install();
		}

		public override void Uninstall()
		{
			_settingService.DeleteSetting<ActiveChatSettings>();
			_activeChatObjectContext.Uninstall();
            this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.StartChat");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.TitleOnline");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.TitleOffline");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldName");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldEmail");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.TextFieldMessage");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.LeaveAMessage");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.Close");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.OperatorTyping");
			this.DeletePluginLocaleResource("Plugins.Widgets.ActiveChat.Send");
			base.Uninstall();
		}
	}
}